<?php
declare (strict_types=1);

namespace app\user\service\user;

use app\common\service\BaseService;
use app\user\logic\user\Account as UserLogic;
use GuzzleHttp\Exception\GuzzleException;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * 用户接口类
 */
class Account extends BaseService
{
    /**
     * @Inject()
     *
     * @var UserLogic
     */
    protected $userLogic;

    /**
     * 修改用户信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     */
    public function updateAccount(): Response
    {
        $data = Request::put();
        $user = $this->userLogic->editUser(request()->user, $data);

        return static::parseToData($user, 1, '修改成功');

    }

    /**
     * 删除用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     */
    public function deleteAccount(): Response
    {
        $res = $this->userLogic->deleteUser(request()->user);

        return static::parseToData($res, 1, '注销账号成功');

    }

    /**
     * 注册用户
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-16
     */
    public function register(): Response
    {
        $data = Request::post();
        $user = $this->userLogic->register($data);

        return static::parseToData($user, 1, '注册成功');
    }

    /**
     * 用户登录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-04
     */
    public function login(): Response
    {
        $data = Request::post();
        $user = $this->userLogic->login($data);

        return static::parseToData($user->toArray(), 1, '登录成功');
    }

    /**
     * 用户三方登录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-04
     */
    public function thirdlogin(): Response
    {
        $param = Request::post();
        $user  = $this->userLogic->thirdlogin($param);

        return static::parseToData($user->toArray(), 1, '登录成功');
    }

    /**
     * 小程序微信登录
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/06/27
     *
     * @return Response
     * @throws GuzzleException
     */
    public function miniappLogin(): Response
    {
        $param = Request::post();
        $user  = $this->userLogic->miniappLogin($param);

        return static::parseToData($user->toArray(), 1, '登录成功');
    }

    /**
     * 用户登录-设置密码
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-11
     */
    public function pwdSet(): Response
    {
        $data = Request::put();
        $user = $this->userLogic->pwdSet(request()->user, $data);

        return static::parseToData($user->toArray(), 1, '设置成功');
    }

    /**
     * 用户登录-修改密码
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-11
     */
    public function pwdEdit(): Response
    {
        $data = Request::put();
        $user = $this->userLogic->pwdEdit(request()->user, $data);

        return static::parseToData($user->toArray(), 1, '修改成功');
    }

    /**
     * 忘记密码-手机验证
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-06
     */
    public function pwdVerify(): Response
    {
        $data = Request::post();
        $re   = $this->userLogic->pwdVerify($data);

        return static::parseToData(['pk' => $re], 1, '验证成功');
    }

    /**
     * 忘记密码-重置密码
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-06
     */
    public function pwdReset(): Response
    {
        $data = Request::put();
        $user = $this->userLogic->pwdReset($data);

        return static::parseToData($user, 1, '重置密码成功');
    }

    /**
     * 忘记密码 - 同步实现（同一页面实现）
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-16
     */
    public function pwdResetSync(): Response
    {
        $data = Request::put();
        $user = $this->userLogic->pwdResetSync($data);

        return static::parseToData($user, 1, '重置密码成功');
    }

    /**
     * 用户信息
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-04-20
     *
     * @return Response
     */
    public function info(): Response
    {
        $user = $this->userLogic->getUserInfo();

        return static::parseToData($user, 1, '获取成功');
    }

    /**
     * 验证手机号
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-04-22
     */
    public function verifyPhone(): Response
    {
        $param = Request::post();
        $phone = $this->userLogic->verifyPhone($param);

        return static::parseToData($phone, 1, '验证成功');
    }

}
